package com.yh.hljapp.gddb.gbmanager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.yh.hljapp.gddb.gdbean.DaoMaster;
import com.yh.hljapp.gddb.gdbean.DaoSession;
import com.yh.hljapp.gddb.gdbean.ScoreEntry;
import com.yh.hljapp.gddb.gdbean.ScoreEntryDao;

import java.util.List;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：ScoreEntryManager
 * <p>
 * 创建时间：2017.05.27.9:24
 * *****************************
 */
public class ScoreEntryManager {

    private final static String dbName = "fb_db";
    private static ScoreEntryManager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;
    private final DaoSession mDaoSession;

    public ScoreEntryManager(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        mDaoSession = daoMaster.newSession();
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static ScoreEntryManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (ScoreEntryManager.class) {
                if (mInstance == null) {
                    mInstance = new ScoreEntryManager(context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }

    //同步积分表
    public boolean syncScoreEntry(List<ScoreEntry> list) {
        ScoreEntryDao scoreEntryDao = mDaoSession.getScoreEntryDao();
        try {
            scoreEntryDao.deleteAll();
            scoreEntryDao.insertOrReplaceInTx(list);
        } catch (Exception e) {
            return false;
        }
        return true;
    }


    public boolean rangeInDefined(int current, int min, int max) {
        return Math.max(min, current) == Math.min(current, max);
    }

    //通过itemid拿到fid最大的集合
    public double getScoreByItemId(String itemId, int count) {
        ScoreEntryDao scoreEntryDao = mDaoSession.getScoreEntryDao();
        String tablename = mDaoSession.getScoreEntryDao().getTablename();
        String sql = "SELECT MAX(FID) AS FID " + tablename + " WHERE FITEM_ID=" + itemId + " ORDER BY DESC";
        List<ScoreEntry> list = scoreEntryDao.queryBuilder().where(ScoreEntryDao.Properties.FItemID.eq(itemId)).orderDesc(ScoreEntryDao.Properties.FID).list();
        double fSaleScore=0;
        if (list != null && list.size() > 0) {
            String maxFid = list.get(0).getFID();
            List<ScoreEntry> list1 = scoreEntryDao.queryBuilder().where(ScoreEntryDao.Properties.FID.eq(maxFid)).orderAsc(ScoreEntryDao.Properties.FEntryID).list();
            if ("0".equals(list1.get(0).getFType())){
                fSaleScore=list1.get(0).getFSaleScore();
                return fSaleScore;
            }
            int min = 0;
            for (int i = 0; i < list1.size(); i++) {
                ScoreEntry scoreEntry = list1.get(i);
                int LimitQty = Integer.parseInt(scoreEntry.getFLimitQty());
                if (rangeInDefined(count,min,LimitQty)){
                    fSaleScore = scoreEntry.getFSaleScore();
                    break;
                }else{
                    min=LimitQty+1;
                }
            }
        }


        return fSaleScore;





//        List<ScoreEntry> datas = new ArrayList<ScoreEntry>();
//        if (list!=null&&list.size()>0){
//            String max = list.get(0).getFID();
//            for (int i = 0; i < list.size(); i++) {
//                ScoreEntry scoreEntry = list.get(i);
//                String fid = scoreEntry.getFID();
//                if (max.equals(fid)){
////                    PLog.i("lj","fentryid="+scoreEntry.getFEntryID());
//                    datas.add(scoreEntry);
//                }
//            }
//        }
//        PLog.i("lj","size="+datas.size());
//        return datas;
    }


}
